package com.example.hp.storeapp.Activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hp.storeapp.Const;
import com.example.hp.storeapp.R;
import com.example.hp.storeapp.adapter.ShopDetailAdapter;
import com.example.hp.storeapp.base.BaseActivity;
import com.example.hp.storeapp.bean.CookResult;
import com.example.hp.storeapp.bean.IsFavorResult;
import com.example.hp.storeapp.bean.NoFavorSResult;
import com.example.hp.storeapp.interf.RetrofitService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShopDetailActivity extends BaseActivity {

    private  int shopId;
    private  String shopName;

    private Retrofit retrofit;
    private TextView titleView;
    private ImageButton favorButton;
    private RecyclerView recyclerView;
    private ShopDetailAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_detail);



        shopId = getIntent().getIntExtra("shop_id",-1);//获取shop_id
        shopName =getIntent().getStringExtra("shop_name");

        titleView = (TextView) findViewById(R.id.textView_title);
        titleView.setText(shopName);

        favorButton = (ImageButton) findViewById(R.id.imageButton_favor);
        favorButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击收藏按钮，改变收藏状态
                doFavor(userId,String.valueOf(shopId));
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        retrofit = new Retrofit.Builder().baseUrl(Const.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
        //加载数据CookListAdapter

        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<CookResult>> call = retrofitService.getFoodByShop(String.valueOf(shopId));
        call.enqueue(new Callback<List<CookResult>>() {
            @Override
            public void onResponse(Call<List<CookResult>> call, Response<List<CookResult>> response) {
                adapter = new ShopDetailAdapter(ShopDetailActivity.this,response.body());
                LinearLayoutManager layoutManager = new LinearLayoutManager(ShopDetailActivity.this);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());

                //获取完店铺详情后，再请求获取收藏状态
                getIsFavor(userId,String.valueOf(shopId));

            }
            @Override
            public void onFailure(Call<List<CookResult>> call, Throwable t) {

            }
        });
    }
            private void getIsFavor(String userId, String shopFoodId){
                //定义Retrofit对象
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                //获取retrofit接口
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);
                Call<IsFavorResult> call = retrofitService.isCollected(userId,shopFoodId,"0");
                call.enqueue(new Callback<IsFavorResult>() {
                    @Override
                    public void onResponse(Call<IsFavorResult> call, Response<IsFavorResult> response) {
                        //获取店铺的收藏
                        if (response.body().getCollected().equals("1")){
                            //已收藏
                            favorButton.setImageResource(R.drawable.ic_collects);
                        }
                        else {
                            favorButton.setImageResource(R.drawable.ic_collects_no);
                        }

                    }

                    @Override
                    public void onFailure(Call<IsFavorResult> call, Throwable t) {

                    }
                });
            }
            private void doFavor(String userId,String shopId){
                //定义Retrofit对象
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                //获取retrofit接口
                RetrofitService retrofitService =
                        retrofit.create(RetrofitService.class);
                Call<NoFavorSResult> call = retrofitService.userCollectShop(userId,shopId);
                call.enqueue(new Callback<NoFavorSResult>() {
                    @Override
                    public void onResponse(Call<NoFavorSResult> call, Response<NoFavorSResult> response) {
                        if (response.body().getSuccess().equals("1")){
                            //操作成功
                            Toast.makeText(ShopDetailActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<NoFavorSResult> call, Throwable t) {

                    }
                });

            }






}
